const axios = require('axios')
const cheerio = require('cheerio')
const download = require('download')
const fs = require('fs')

async function getData() {
  const res = await axios.get('https://www.itheima.com/teacher.html#aweb')
  // console.log(res.data)
  const $ = cheerio.load(res.data)
  const imgReg = /\.jpg|jpeg|png|bmp|gif$/
  const imgArr = Array.from($('.tea_main .tea_con img')).map(item => encodeURI('https://www.itheima.com/' + $(item).attr('src'))).filter(item => imgReg.test(item))
  // console.log(imgArr)
  if (!fs.existsSync('./dist')) fs.mkdirSync('./dist')
  imgArr.forEach(async img => fs.writeFileSync('dist/' + decodeURI(img).split('/').pop(), await download(img)))
}

getData()
